package com.qlm.tool;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.UUID;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;


public class WxUtil {
	private static String charset = "UTF-8";
    /**
     * 日期格式
     */
    public static String PATTEN_YMD = "yyyy-MM-dd";
    /**
     * 获取当前日期字符串
     * 
     * @param formart 日期格式
     * @return 当前日期字符串
     */
    public static String getDateFormatStr(String formart) {
        return new SimpleDateFormat(formart).format(new java.util.Date());
    }
    
    public static void main(String[] args) {
		System.out.println(getOrderNo());;
	}
	public static String getOrderNo() {
		String order = new SimpleDateFormat("yyyyMMddhhmmssSSS").format(new Date());
		Random r = new Random();
		for (int i = 0; i < 15; i++) {
			order += r.nextInt(9);
		}
		return order;
	}
	
	public static String getUUID(){
		String uuid = UUID.randomUUID().toString(); //获取UUID并转化为String对象  
        uuid = uuid.replace("-", "");  
        return uuid;
	}
	
	public static String hbValue(int amount){
		double doublecount = new BigDecimal(amount).divide(new BigDecimal(100)).doubleValue();
		String result = String.format("%.2f",doublecount);
		return result;
	}
	
	public static boolean compareStr(String str1,String str2){
		if(str1 == null || str2 == null){
			return false;
		}
		if(str1.equals(str2)){
			return true;
		}
		return false;
	}
	
	public static Record cloneRecord(Record ori){
		if(ori == null){
			return null;
		}else{
			Record newr = new Record();
			Map<String, Object> columns = ori.getColumns();
			newr.setColumns(columns);
			return newr;
		}
	}
	
	public static boolean isNull(String str){
		if(str == null || "".equals(str)){
			return true;
		}
		return false;
	}
    /**
     * 获取当前时间，字符串类型，格式（yyyy-mm-dd）
     * 
     * @return String
     */
    public static String getCurrentDateString() {
        return getDateFormatStr(PATTEN_YMD);
    }
    
    public static String getRandomString(int length) { //length表示生成字符串的长度
	    String base = "abcdefghijklmnopqrstuvwxyz0123456789";   
	    Random random = new Random();   
	    StringBuffer sb = new StringBuffer();   
	    for (int i = 0; i < length; i++) {   
	        int number = random.nextInt(base.length());   
	        sb.append(base.charAt(number));   
	    }   
	    return sb.toString();   
	 }  
	/**
	 * 随机16为数值
	 * 
	 * @return
	 */
	public static String buildRandom() {
		String currTime = getCurrentDateString();
		String strTime = currTime.substring(8, currTime.length());
		int num = 1;
		double random = Math.random();
		if (random < 0.1) {
			random = random + 0.1;
		}
		
		for (int i = 0; i < 4; i++) {
			num = num * 10;
		}
		return (int) ((random * num)) + strTime;
	}
	public static String createSign(Map<String, String> map,String partnerKey) {
		TreeMap<String, String> packageParams = new TreeMap<String, String>(map);
		// 去除参与的参数sign
		packageParams.remove("sign");
		// 遍历排序后的字典，将所有参数按"key=value"格式拼接在一起
				StringBuilder sb = new StringBuilder();
				boolean first = true;
		for (Map.Entry<String, String> param : packageParams.entrySet()) {
			String value = param.getValue();
			if (StrKit.isBlank(value)) {
				continue;
			}
			if (first) {
				first = false;
			} else {
				sb.append("&");
			}
			sb.append(param.getKey()).append("=");
//			if (true) {
//				try { value = URLEncoder.encode(value, charset).replace("+", "%20"); } catch (UnsupportedEncodingException e) {}
//			}
			sb.append(value);
		}
		String sign = MD5Util.MD5Encode(sb.toString()+"&key="+partnerKey, charset).toUpperCase();
		return sign;
	}
	
	/**
	 * 获取整型  可以保证正确的获取数据库正确的整型
	 * @param record
	 * @return
	 */
	public static Integer getInt(String key , Record record){
		Object object = record.get(key);
		return getInt(object);
	}
	
	public static Integer getInt(String key , Record record,int defal){
		Object object = record.get(key);
		return getInt(object,defal);
	}
	
	public static Integer getInt(String key , Model<?> model,int defal){
		Object object = model.get(key);
		return getInt(object,defal);
	}
	
	/**
	 * 获取整型  可以保证正确的获取数据库正确的整型
	 * @param key
	 * @param model
	 * @return
	 */
	public static Integer getInt(String key ,Model<?> model){
		Object object = model.get(key);
		return getInt(object);
	}
	
	private static Integer getInt(Object obj){
		if(obj == null) return 0;
		Integer result = 0;
		if(obj instanceof Number){
			Number cast = Number.class.cast(obj);
			result = cast.intValue();
		}else{
			result = null;
		}
		return result;
	}
	
	private static Integer getInt(Object obj,int defaultVal){
		if(obj == null) return defaultVal;
		Integer result = 0;
		if(obj instanceof Number){
			Number cast = Number.class.cast(obj);
			result = cast.intValue();
		}else{
			result = defaultVal;
		}
		return result;
	}
	
	/**
	 * 获取String
	 * @param key
	 * @param model
	 * @return
	 */
	public  static  String getStr(String key,Model<?> model){
		Object object = model.get(key);
		if(object == null){
			return null;
		}else if(object instanceof String){
			return (String)object;
		}
		return null;
	}
	
	public  static String getStr(String key,Record model){
		Object object = model.get(key);
		if(object == null){
			return null;
		}else if(object instanceof String){
			return (String)object;
		}
		return null;
	}
	/**
	 * 获取值，如果为null，返回默认值
	 * @param key
	 * @param model
	 * @param defaultValue
	 * @return
	 */
	public static String getStr(String key,Model<?> model,String defaultValue){
		Object object = model.get(key);
		if(object == null){
			return defaultValue;
		}else if(object instanceof String){
			return (String)object;
		}
		return defaultValue;
	}
	
	public static String getStr(String key, Record model,String defaultValue){
		Object object = model.get(key);
		if(object == null){
			return defaultValue;
		}else if(object instanceof String){
			return (String)object;
		}
		return defaultValue;
	}
	
	public static Integer parseInt(String key,int defal){
		try{
			int parseInt = Integer.parseInt(key);
			return parseInt;
		}catch(Exception e){
			return defal;
		}
	}
	
	public static Integer parseInt(String key){
		return parseInt(key,-1);
	}
}
